﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using PanasonicWarranty.Business;
using System.Web.UI.HtmlControls;
using Utils;
using System.Data;
namespace PanasonicWarranty.UI
{
    public partial class LookupWarranty : BasePageManageWarranty
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
        }

        void paging_PagingChange(object sender, EventArgs e)
        {
            pagingtop.CurrentPage = (sender as UserControl.ucPaging).CurrentPage;
            pagingbottom.CurrentPage = (sender as UserControl.ucPaging).CurrentPage;
            BindGridData();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageSize = Convert.ToInt32(tSortable_length.SelectedValue);
                pagingtop.CurrentPage = pagingbottom.CurrentPage = 1;
                pagingtop.PageSize = pagingbottom.PageSize = PageSize;
                pagingtop.PageDisplay = pagingbottom.PageDisplay = 6;
            }
        }

        private void BindGridData()
        {
            try
            {
                if (!CheckSearch()) return;
                int currentPage = pagingtop.CurrentPage;
                int pageSize = pagingtop.PageSize;
                int totalRow = 0;
                string model = txtSearchModel.Text.Trim();
                string productcode = txtSearchProductCode.Text.Trim();
                string phone = txtSearchPhone.Text.Trim();
                grvData.DataSource = pa_WarrantyDetail.Paging(currentPage, pageSize, ref totalRow, null, model, productcode, phone, null, null);
                pagingtop.TotalRecord = pagingbottom.TotalRecord = totalRow;
                pagingtop.Visible = pagingbottom.Visible = pagingtop.TotalPage > 1;
                grvData.DataBind();
                ltrInfo.Text = "Tổng cộng: " + totalRow;
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            pagingtop.PagingChange -= new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange -= new EventHandler(paging_PagingChange);
            pagingtop.CurrentPage = pagingbottom.CurrentPage = 1;
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
            BindGridData();
        }

        protected void tSortable_length_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                PageSize = Convert.ToInt32(tSortable_length.SelectedValue);
                pagingtop.PageSize = pagingbottom.PageSize = PageSize;
                BindGridData();
            }
            catch { }
        }


        private bool CheckSearch()
        {
            try
            {
                if (string.IsNullOrWhiteSpace(txtSearchPhone.Text) && string.IsNullOrWhiteSpace(txtSearchProductCode.Text) && txtSearchModel.Text.Trim() == "")
                {
                    throw new Exception("Điều kiện tìm kiếm không được để trống");
                }
                if (txtSearchModel.Text.Trim() != "")
                {
                    if (string.IsNullOrWhiteSpace(txtSearchProductCode.Text))
                        throw new Exception("Phải tìm kiếm theo cả model và số máy");
                }
                ltrCheckSearch.Text = "";
                return true;
            }
            catch (Exception e)
            {
                ltrCheckSearch.Text = e.Message;
                return false;
            }
            
        }
    }
}